<script setup lang="ts">

import { addPatient, delPatient, editPatient, getPatientList } from '@/services/user';
import type { Patient, PatientList } from '@/types/user';
// import { isTemplateElement } from '@babel/types';
import { showToast } from 'vant';
import { computed, onMounted, ref } from 'vue';
import cp from '../../components/cp-nav-bar.vue'
import btn from '../../components/cp-radio-btn.vue'

const list = ref<PatientList>()
onMounted(() => {
  listPatient()
})
const listPatient = async () => {
  const res = await getPatientList()
  list.value = res.data
}
const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]
// const gender = ref(1)
const show = ref(false)
// 弹窗
const showPopup = (item?: Patient) => {
  // 区分新增还是编辑
  if (item) {
    // 编辑
    const { id, gender, name, idCard, defaultFlag, value } = item
    patient.value = { id, gender, name, idCard, defaultFlag, value }
  } else {
    // 新增
    // 点击弹窗, 可以在这进行数据重置
    patient.value = { ...patientInit }
  }
  // // 然后再弹窗
  show.value = true
}
// 表单数据绑定
const patientInit: Patient = {
  name: '',
  idCard: '',
  gender: 1,
  defaultFlag: 0,
  // id: '',
  value: undefined
}
const patient = ref<Patient>({ ...patientInit })
// 因为传过来的数据defaultflag和后端不一样所以就要做处理
const defaultFlag = computed({
  get: () => {
    return patient.value.defaultFlag == 1 ? true : false
  },
  set: (val) => {
    patient.value.defaultFlag = val ? 1 : 0
  }
})
// 添加或者编辑
const submit = async () => {
  if (patient.value.id) {
    await editPatient(patient.value)
  } else {
    await addPatient(patient.value)

  }


  show.value = false
  listPatient()
  showToast('添加成功')
}
// 删除
const remove = async (id: string | number) => {
  await delPatient(id)
  show.value = false
  listPatient()
  showToast('删除成功')
}
</script>

<template>
  <div class="patient-page">
    <cp title="家庭档案"></cp>
    <div class="patient-list">
      <div class="patient-item" v-for="item in list" :key="item.id">
        <div class=" info">
          <!-- 因为返回的是数组，所以要遍历 -->
          <span class="name">{{ item.name }}</span>
          <span class="id">321***********6164</span>
          <span>男</span>
          <span>32岁</span>
        </div>
        <div class="icon" @click="showPopup(item)"><cp-icons name="user-edit" /></div>
        <div class="tag">默认</div>
      </div>

      <div class="patient-add">
        <cp-icons name="user-add" @click="showPopup" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
      <cp-radio-btn :options="options" v-model="patient.gender" />

      <!-- 侧边栏 -->
      <!-- 侧边栏 -->
      <van-popup v-model:show="show" position="right">
        <cp-nav-bar :back="
          () => {
            show = false
          }
        " :title="patient.id ? '编辑患者' : '新增患者'" right-text="保存" @click-right="submit"></cp-nav-bar>
        <van-form autocomplete="off">
          <van-field v-model="patient.name" label="真实姓名" placeholder="请输入真实姓名" />
          <van-field v-model="patient.idCard" label="身份证号" placeholder="请输入身份证号" />
          <van-field label="性别">
            <!-- 单选按钮组件 -->
            <template #input>
              <btn :options="options" v-model="patient.gender"></btn>
            </template>
          </van-field>
          <van-field label="默认就诊人">
            <template #input>
              <van-checkbox round v-model="defaultFlag" />
            </template>
          </van-field>
          <van-action-bar>
            <van-action-bar-button @click="remove(patient.id)">删除</van-action-bar-button>
          </van-action-bar>
        </van-form>


      </van-popup>


      123


    </div>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;

  ::v-deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}

.patient-list {
  padding: 15px;
}

.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;

  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;

    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;

      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }

      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }

  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }

  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);

    .icon {
      color: var(--cp-primary);
    }
  }
}

.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;

  .cp-icon {
    font-size: 24px;
  }
}

.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
</style>
